﻿Imports EVOLTA.Promocion.Bl.Bc
Imports EVOLTA.Promocion.Bl.Be

Partial Class mantenedores_vendedor
    Inherits System.Web.UI.Page

    Private vendedors As List(Of Evendedor) = Nothing
    Dim expression As String = Nothing
    Dim direction As SortDirection = SortDirection.Ascending

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        'Dim script As String = "<script languaje=\'javascript\'>limpiarControles();</script>"
        'ClientScript.RegisterStartupScript(Me.GetType(), "IMPRESO", script)

        Me.Page.ClientScript.GetPostBackClientHyperlink(Me.btnGuardar, String.Empty)
        Dim sb As New StringBuilder
        sb.Append(Me.Page.ClientScript.GetPostBackEventReference(Me.btnGuardar, vbNullString))
        sb.Append(";")
        btnGuardar.Attributes("onclick") = sb.ToString()

        btnAgregar.Attributes.Add("onclick", "MuestraPanel(); return false;")
        If Not IsPostBack Then
            cargarvendedores()
            cargarTpoDmto()
            cargarEstado()
        End If
        'Dim scriptText As String = ""
        'scriptText &= "function cargar(){"
        'scriptText &= "    limpiarControles(); "
        'scriptText &= "}"
        'ClientScript.RegisterClientScriptBlock(Me.GetType(), "limpiar()", scriptText, True)
    End Sub
    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        gvwLista.DataSource = Buscar(txtBuscar.Value.ToUpper.Trim)
        gvwLista.DataBind()
    End Sub
    Private Function Buscar(ByVal filtro As String) As IEnumerable(Of Evendedor)
        Dim Lvendedor As System.Collections.Generic.IEnumerable(Of Evendedor) = Nothing
        vendedors = Session("lista")
        If Not vendedors Is Nothing Then
            If filtro.Length > 0 Then
                Dim x As Int32 = Asc(filtro.Substring(0, 1).Trim)
                If x >= 48 AndAlso x <= 57 Then
                    filtro = CType(filtro, Int32)
                    Lvendedor = From v In vendedors _
                                Where v.idVendedor = filtro _
                                Select v
                    Return Lvendedor
                Else
                    Lvendedor = From v In vendedors _
                                Where v.NombresCompletos Like "*" + filtro + "*" _
                                Select v
                End If
            Else
                Lvendedor = From v In vendedors _
                            Where v.NombresCompletos Like "*" + filtro + "*" _
                            Select v
            End If
        End If
        Return Lvendedor
    End Function
    Protected Sub lnkBtn_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) _
    Handles lnkBtnCodigo.Command, lnkBtnNombre.Command, lnkBtnTelefono.Command, lnkBtnEmail.Command, lnkBtnEstado.Command
        Dim vendedor As IEnumerable(Of Evendedor) = Buscar(txtBuscar.Value.ToUpper.Trim)

        If Not vendedor Is Nothing Then
            Select Case e.CommandName
                Case "idVendedor"
                    Dim List = From d In vendedor _
                               Order By d.idVendedor _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "NombresCompletos" : expression = e.CommandArgument
                    Dim List = From d In vendedor _
                               Order By d.NombresCompletos _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "telefono" : expression = e.CommandArgument
                    Dim List = From d In vendedor _
                               Order By d.telefono _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "email" : expression = e.CommandArgument
                    Dim List = From d In vendedor _
                               Order By d.email _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "descEstado" : expression = e.CommandArgument
                    Dim List = From d In vendedor _
                               Order By d.estado _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
            End Select
        End If
    End Sub
    Private Sub cargarvendedores()
        vendedors = Rvendedor.SeleccionarTodo(Me.Master.descEmpresa, False)
        Session("lista") = vendedors
        If Not vendedors Is Nothing Then
            gvwLista.DataSource = vendedors
            gvwLista.DataBind()
        End If
        'Dim xmlDoc As New System.Xml.XmlDocument
        'xmlDoc.Load(Rvendedor.SeleccionarTodoXML(Me.Master.descEmpresa, False))
        'Repeater1.DataSource = xmlDoc

    End Sub
    Private Sub cargarTpoDmto()
        selTpoDcmto.DataSource = RtpoDcmto.SeleccionarTodo(Me.Master.descEmpresa, True)
        selTpoDcmto.DataTextField = "descTpoDcmto"
        selTpoDcmto.DataValueField = "idTpoDcmto"
        selTpoDcmto.DataBind()
    End Sub
    Private Sub cargarEstado()
        Dim Eestado As New Ecommons
        selEstado.DataSource = Eestado.estado
        selEstado.DataTextField = "descripcion"
        selEstado.DataValueField = "codigo"
        selEstado.DataBind()
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwLista.PageIndexChanging
        'Me.gvwLista.PageIndex = e.NewPageIndex
        'Me.gvwLista.DataBind()
    End Sub
    Protected Sub gvwLista_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
    Handles gvwLista.RowCommand
        Select Case e.CommandName
            Case "Edit"
                'Dim id As String = e.CommandArgument
            Case "Delete"
                Dim pVendedor As Evendedor = New Evendedor
                With pVendedor
                    .idVendedor = e.CommandArgument
                    .idUsuario = Me.Master.idUsuario
                End With
                Rvendedor.Eliminar(Me.Master.descEmpresa, pVendedor)
                cargarvendedores()
        End Select
    End Sub
    Protected Sub gvwLista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
    Handles gvwLista.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gvwLista, "Select$" + e.Row.RowIndex.ToString)
            e.Row.Style.Add("cursor", "hand")
        End If
    End Sub
    Protected Sub gvwLista_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwLista.RowDeleting
        'gvwLista.Visible = countList > 0
    End Sub
    Protected Sub gvwLista_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvwLista.RowEditing
        Dim id As Int32 = CType(CType(gvwLista.Rows(e.NewEditIndex).FindControl("lnkCodigo"), LinkButton).Text, Int32)
        vendedors = Session("lista")
        Dim Lvendedor = From v In vendedors _
                            Where v.idVendedor.ToString = id _
                            Select v
        lblTitulo.Text = "Vendedor [Modificar]"
        lblTitulo.ToolTip = "Modificar"
        txtId.Text = Lvendedor.ElementAt(0).idVendedor
        txtNombre.Value = Lvendedor.ElementAt(0).nombre
        txtApePaterno.Value = Lvendedor.ElementAt(0).apePaterno
        txtApeMaterno.Value = Lvendedor.ElementAt(0).apeMaterno
        selTpoDcmto.SelectedIndex = Lvendedor.ElementAt(0).EtpoDocmto.idTpoDcmto
        txtNroDocumento.Value = Lvendedor.ElementAt(0).nroDcmto
        txtTelefonoFijo.Text = Lvendedor.ElementAt(0).telefono
        txtTelefonoCelular.Text = Lvendedor.ElementAt(0).movil
        txtcomisionProforma.Value = Lvendedor.ElementAt(0).comisionProforma
        txtcomisionSeparacion.Value = Lvendedor.ElementAt(0).comisionSeparacion
        txtcomisionVenta.Value = Lvendedor.ElementAt(0).comisionVenta
        txtEmail.Value = Lvendedor.ElementAt(0).email
        selEstado.SelectedIndex = Lvendedor.ElementAt(0).estado
        mpDetalle.Show()
    End Sub
    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If txtNombre.Value.Trim.Length > 0 AndAlso txtApeMaterno.Value.Trim.Length > 0 AndAlso txtApePaterno.Value.Trim.Length > 0 Then
            Dim jsMensaje As String = Nothing
            Dim pVendedor As Evendedor = Nothing
            If lblTitulo.ToolTip = "Registrar" Then
                pVendedor = New Evendedor
                With pVendedor
                    .nombre = txtNombre.Value.Trim.ToUpper
                    .apePaterno = txtApePaterno.Value.Trim.ToUpper
                    .apeMaterno = txtApeMaterno.Value.Trim.ToUpper
                    .EtpoDocmto = New EtpoDcmto
                    .EtpoDocmto.idTpoDcmto = selTpoDcmto.Value
                    .nroDcmto = txtNroDocumento.Value
                    .movil = txtTelefonoCelular.Text.Trim.ToUpper
                    .telefono = txtTelefonoFijo.Text.Trim.ToUpper
                    .comisionProforma = txtcomisionProforma.Value.Trim.ToUpper
                    .comisionSeparacion = txtcomisionSeparacion.Value.Trim.ToUpper
                    .comisionVenta = txtcomisionVenta.Value.Trim.ToUpper
                    .email = txtEmail.Value.Trim.ToUpper
                    .idUsuario = Me.Master.idUsuario
                End With
                Dim resultado As Boolean = Rvendedor.Añadir(Me.Master.descEmpresa, pVendedor)
                If resultado Then
                    jsMensaje = "<script>javascript:alert(' Se registro correctamente.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                    cargarvendedores()
                Else
                    jsMensaje = "<script>javascript:alert('No se pudo registrar.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                End If
            ElseIf lblTitulo.ToolTip = "Modificar" Then
                pVendedor = New Evendedor
                With pVendedor
                    .idVendedor = CType(txtId.Text, Int32)
                    .nombre = txtNombre.Value.Trim.ToUpper
                    .apePaterno = txtApePaterno.Value.Trim.ToUpper
                    .apeMaterno = txtApeMaterno.Value.Trim.ToUpper
                    .EtpoDocmto = New EtpoDcmto
                    .EtpoDocmto.idTpoDcmto = selTpoDcmto.Value
                    .nroDcmto = txtNroDocumento.Value
                    .movil = txtTelefonoCelular.Text.Trim.ToUpper
                    .telefono = txtTelefonoFijo.Text.Trim.ToUpper
                    .comisionProforma = txtcomisionProforma.Value.Trim.ToUpper
                    .comisionSeparacion = txtcomisionSeparacion.Value.Trim.ToUpper
                    .comisionVenta = txtcomisionVenta.Value.Trim.ToUpper
                    .email = txtEmail.Value.Trim.ToUpper
                    .estado = selEstado.Value
                    .idUsuario = Me.Master.idUsuario
                End With
                Rvendedor.Editar(Me.Master.descEmpresa, pVendedor)
                jsMensaje = "<script>javascript:alert(' Se modifico correctamente.');</script>)"
                Controls.Add(New LiteralControl(jsMensaje))
                cargarvendedores()
            End If
            lblTitulo.ToolTip = "Registrar"
        End If
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        lblTitulo.ToolTip = "Registrar"
        mpDetalle.CancelControlID = "btnCancelar"
    End Sub
    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        vendedors = Session("lista")
        If Not vendedors Is Nothing Then
            Dim fileName As String = "Consulta{0}.xls"
            fileName = String.Format(fileName, DateTime.Now.ToShortDateString())
            Dim Response As HttpResponse = HttpContext.Current.Response
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

            Response.Write("id")
            Response.Write(vbTab)
            Response.Write("Nombres")
            Response.Write(vbTab)
            Response.Write("Apellido Paterno")
            Response.Write(vbTab)
            Response.Write("Apellido Materno")
            Response.Write(vbTab)
            Response.Write("Telefono")
            Response.Write(vbTab)
            Response.Write("Email")
            Response.Write(vbTab)
            Response.Write("Movil")
            Response.Write(vbTab)
            Response.Write("Estado Vendedor")
            Response.Write(vbTab)
            Response.Write("Tipo Documento")
            Response.Write(vbTab)
            Response.Write("Nro de Documento")
            Response.Write(vbTab)
            Response.Write("Comision Proforma")
            Response.Write(vbTab)
            Response.Write("Comision Separación")
            Response.Write(vbTab)
            Response.Write("Comision Venta")
            Response.Write(vbTab)
            Response.Write(vbCrLf)

            Dim value As String
            For Each vendedor In vendedors
                value = vendedor.idVendedor
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.apePaterno
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.apeMaterno
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.nombre
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.telefono
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.email
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.movil
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.descEstado
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.EtpoDocmto.descTpoDcmto
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.nroDcmto
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.comisionProforma
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.comisionSeparacion
                Response.Write(value)
                Response.Write(vbTab)

                value = vendedor.comisionVenta
                Response.Write(value)
                Response.Write(vbCrLf)
            Next
            Response.End()
        Else
            Dim jsMensaje As String = Nothing
            jsMensaje = "<script>javascript:alert(' La consulta no pudo ser procesada.  Por favor inténtelo de nuevo.');</script>)"
            Controls.Add(New LiteralControl(jsMensaje))
        End If
    End Sub

End Class
